package com.ruoyi.web.controller.okr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.KanbanData;
import com.ruoyi.system.domain.OkrCycle;
import com.ruoyi.system.domain.OkrInfo;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.resultMapper.OkrAnalysisResult;
import com.ruoyi.system.domain.resultMapper.OkrCountResult;
import com.ruoyi.system.domain.resultMapper.OkrStatusResult;
import com.ruoyi.system.service.IOkrCycleService;
import com.ruoyi.system.service.IOkrInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bruce on 2021/9/12 12:27
 **/
@Controller
@RequestMapping("/operating")
public class MyOkrOperatingController extends BaseController {

    String prefix = "okr";

    @Autowired
    private IOkrInfoService okrInfoService;

    @Autowired
    private IOkrCycleService okrCycleService;

    Long cycleId;
    Long cycleId_1;
    Long workStatus;

    @GetMapping("")
    public String show(ModelMap mmap){
        List<OkrCycle> list = okrCycleService.selectOkrCycleList(new OkrCycle());
        mmap.put("cycleList", list);
        return prefix + "/okrOperating";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo getOperatingList(OkrInfo okrInfo, ModelMap mmap){
        SysUser user = ShiroUtils.getSysUser();
        Long cycleId = okrInfo.getCycleId();
        if (cycleId == null){
            List<OkrCycle> useCycleList = okrCycleService.selectOkrUseCycleList();
            cycleId = useCycleList.get(0).getId();
        }
        List<OkrAnalysisResult> okrInfos = okrInfoService.queryOkrInDept(user.getDeptId(), cycleId, 1L);
        OkrStatusResult okrStatus_1 = new OkrStatusResult(1L,"正常");
        OkrStatusResult okrStatus_2 = new OkrStatusResult(2L,"有风险");
        OkrStatusResult okrStatus_3 = new OkrStatusResult(3L,"已延期");
        okrStatus_1.setCount(0);
        okrStatus_2.setCount(0);
        okrStatus_3.setCount(0);
        for (OkrAnalysisResult kr : okrInfos){
            if (kr.getStatus() == 1){
                okrStatus_1.setCount(okrStatus_1.getCount() + 1);
            }else if (kr.getStatus() == 2){
                okrStatus_2.setCount(okrStatus_2.getCount() + 1);
            }else{
                okrStatus_3.setCount(okrStatus_3.getCount() + 1);
            }
        }
        double sum = okrStatus_1.getCount() + okrStatus_2.getCount() + okrStatus_3.getCount();
        okrStatus_1.setProportion((okrStatus_1.getCount()/sum) * 100);
        okrStatus_2.setProportion((okrStatus_2.getCount()/sum) * 100);
        okrStatus_3.setProportion((okrStatus_3.getCount()/sum) * 100);
        List<OkrStatusResult> results = new ArrayList<>();
        results.add(okrStatus_1);
        results.add(okrStatus_2);
        results.add(okrStatus_3);

        return getDataTable(results);
    }

    @PostMapping("/getCycleId")
    @ResponseBody
    public AjaxResult getCycleId(Long cycleId){
        this.cycleId = cycleId;
        System.out.println("getCycleId: " + this.cycleId);
        if (cycleId != null){
            return AjaxResult.success("获取到周期Id");
        }else {
            return AjaxResult.error("未获取到周期Id");
        }
    }

    @PostMapping("/getCycleId_1")
    @ResponseBody
    public AjaxResult getCycleId_1(Long cycleId){
        this.cycleId_1 = cycleId;
        if (cycleId != null){
            return AjaxResult.success("获取到周期Id");
        }else {
            return AjaxResult.error("未获取到周期Id");
        }
    }

    @PostMapping("/getWorkStatus")
    @ResponseBody
    public AjaxResult getWorkStatus(Long workStatus){
        this.workStatus = workStatus;
        if (workStatus != null){
            return AjaxResult.success("获取到状态Id");
        }else {
            return AjaxResult.error("未获取到状态Id");
        }
    }

    @GetMapping("/getKrs/{workStatus}")
    public String getKrs(@PathVariable("workStatus") Long workStatus){
        this.workStatus = workStatus;
        return prefix + "/OperatingKrs";
    }

    @PostMapping("/getKrsList")
    @ResponseBody
    public TableDataInfo getKrsList(Long cycleId){
        System.out.println(this.cycleId);
        List<OkrInfo> okrInfos = okrInfoService.selectKrInfoByWorkStatus(ShiroUtils.getSysUser().getDeptId(), this.cycleId, workStatus);
        return getDataTable(okrInfos);
    }

    @PostMapping("/getParentOkr")
    @ResponseBody
    public Long getParentOkr(Long krId){
        OkrInfo okrInfo = okrInfoService.selectOkrInfoById(krId);
        OkrInfo info = okrInfoService.selectOkrInfoByParentId(okrInfo.getParentId());
        return info.getId();
    }

    @PostMapping("/updateCountList")
    @ResponseBody
    public TableDataInfo getUpdateCountList(Long cycleId){
        if (cycleId == null){
            List<OkrCycle> useCycleList = okrCycleService.selectOkrUseCycleList();
            cycleId = useCycleList.get(0).getId();
        }
        SysUser user = ShiroUtils.getSysUser();
        List<KanbanData> list = okrInfoService.queryOkrUpdateCount(cycleId, user.getDeptId());
        return getDataTable(list);
    }

    @PostMapping("/getList")
    @ResponseBody
    public List<OkrCountResult> getList(Long cycleId){
        List<OkrCountResult> res = new ArrayList<>();
        SysUser user = ShiroUtils.getSysUser();
        System.out.println("UpdateCount " + cycleId);
        List<KanbanData> list = okrInfoService.queryOkrUpdateCount(cycleId, user.getDeptId());
        OkrCountResult result1 = new OkrCountResult("从未更新", 0);
        OkrCountResult result2 = new OkrCountResult("1-4次",0);
        OkrCountResult result3 = new OkrCountResult("5-8次",0);
        OkrCountResult result4 = new OkrCountResult("9-12次",0);
        OkrCountResult result5 = new OkrCountResult("13次以上",0);

        for (KanbanData data : list){
            int count = data.getUpdateCount();
            if(count == 0){
                result1.setValue(result1.getValue() + 1);
            }else if (count >= 1 && count <= 4){
                result2.setValue(result2.getValue() + 1);
            }else if (count >= 5 && count <= 8){
                result3.setValue(result3.getValue() + 1);
            }else if (count >= 9 && count <= 12){
                result4.setValue(result4.getValue() + 1);
            }else if (count > 13){
                result5.setValue(result5.getValue() + 1);
            }
        }
        res.add(result1);
        res.add(result2);
        res.add(result3);
        res.add(result4);
        res.add(result5);
        return res;
    }




}
